<?php
/**
 * Created by PhpStorm.
 * User: Tangyijun
 * Date: 2017/3/24
 * Time: 16:25
 */
namespace app\home\controller;
use app\models\Bases;

class Chat extends Base{
    /**
     * @return \think\response\Json
     * 保存聊天记录
     */
    public function addChat(){
        $chat = new Bases('chat');
        try{
            $post = input('param.');
            $post['create_time'] = time();
            $id = $chat->insertData($post);
            if(!$id)
                throw new \LogicException('操作失败',10023);
        }catch(\LogicException $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
        return json(self::formatSuccessResult(['chat_id'=>$id]));
    }

    /**
     * @return \think\response\Json
     * 审核接口
     */
    public function verify(){
        $chat = new Bases('chat');
        try{
            $status = input('param.status');
            $chat_id = input("param.chat_id");
            if($chat->setField("id = $chat_id",['status'=>$status])===false){
                throw new \LogicException('审核失败',10024);
            }
        }catch(\Exception $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
        return json(self::formatSuccessResult());
    }

    /**
     * @return \think\response\Json
     * 查询
     */
    public function getChat(){
        $chat = new Bases('chat');
        try{
            $start_time = strtotime(date('y-m-d',time()));
            $end_time = $start_time + 3600*24;
            $result['is_verify']     = $chat->selectData("status = 1 and create_time >= $start_time and create_time <= $end_time","","id desc","0,20");
            $result['is_not_verify'] = $chat->selectData("status = 0 and create_time >= $start_time and create_time <= $end_time","","id desc","");
            if(!$result)
                throw new \LogicException('没有数据',10027);
            foreach($result['is_verify'] as $k=>$v)
                $result['is_verify'][$k]['content'] = html_entity_decode($v['content']); //将实体标签转换成html标签
            foreach($result['is_not_verify'] as $k=>$v)
                $result['is_not_verify'][$k]['content'] = html_entity_decode($v['content']);
        }catch(\Exception $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
        return json(self::formatSuccessResult($result));
    }

    /**
     * @return \think\response\Json
     * 添加文字直播
     */
    public function addTextChat(){
        $textChat = new Bases('text_play');
        try{
            $post = input('param.');
            if(empty(trim($post['content'])))
                throw new \LogicException('聊天内容不能为空');
            $post['create_time'] = time();
            if(!$textChat->insertData($post))
                throw new \LogicException('添加失败',10029);
        }catch(\Exception $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
        return json(self::formatSuccessResult());
    }

    /**
     * @return \think\response\Json
     * 获取文字直播的类容，默认只读取今天一天的数据
     */
    public function getTextChat(){
        $textChat = new Bases('text_play');
        try{
            $start_time = strtotime(date('Y-m-d'));
            $end_time  = $start_time + 3600*24;
            $result = $textChat->selectData("create_time >= $start_time and create_time < $end_time");
            if(!$result){
                throw new \LogicException('没有数据',10030);
            }
            foreach($result as $k=>$v){
                $result[$k]['content'] = html_entity_decode($v['content']);
            }
        }catch(\Exception $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
        return json(self::formatSuccessResult($result));
    }
    /***
     * 敏感词过滤
     */
    public function sensitivWordFiltering(){
        $config = new Bases('config');
        if(request()->isPost()){
            $str = input('param.str');
            $strArr = $config->find("cname = 'chat_sensitive_words' ");
            if(empty($strArr)){
                return $str;
            }
            $chatStr = explode(',',$strArr['option']);

            $result = $this->str($chatStr,$str);
            if(empty($result)){
                return $str;
            }
            return $result;
        }

    }

    function str($chatStr,$str){
        foreach($chatStr as $v){
            static $str1;
            if(strpos($str,$v) !== false){
                $str1 = str_replace($v,'***',$str);
                $this->str($chatStr,$str1);
            }
        }
        return $str1;
    }

}